//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package event_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/events_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type ProjectDayEventStatusItemDbItem struct {
	DayEventStatusItemId string `bson:"_id"`
	ProjectId            string `bson:"projectId"`
	MemberUserId         string `bson:"memberUserId"`
	Day                  uint32 `bson:"day"`
	EventType            int32  `bson:"eventType"`
	Count                uint32 `bson:"count"`
}

func (item *ProjectDayEventStatusItemDbItem) ToDayEventStatusItem() *events_api.DayEventStatusItem {
	return &events_api.DayEventStatusItem{
		Day:       item.Day,
		EventType: events_api.EVENT_TYPE(item.EventType),
		Count:     item.Count,
	}
}

type _ProjectDayEventStatusDao struct {
	mc *mongo.Collection
}

func newProjectDayEventStatusDao(db *mongo.Database) (*_ProjectDayEventStatusDao, error) {
	mc := db.Collection(db_ns.DB_NS_EVENT + "project_day_status")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"memberUserId", 1}, {"day", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_ProjectDayEventStatusDao{mc: mc}, nil
}

func (db *_ProjectDayEventStatusDao) Exist(ctx context.Context, projectId, memberUserId string, day uint32, eventType events_api.EVENT_TYPE) (bool, error) {
	dayEventStatusItemId := db.genDayEventStatusItemId(projectId, memberUserId, day, int32(eventType))
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": dayEventStatusItemId, "projectId": projectId})
	if err != nil {
		return false, err
	}
	return count > 0, nil
}

func (db *_ProjectDayEventStatusDao) genDayEventStatusItemId(projectId, memberUserId string, day uint32, eventType int32) string {
	return fmt.Sprintf("%s:%s:%d:%d", projectId, memberUserId, day, eventType)
}

func (db *_ProjectDayEventStatusDao) Insert(ctx context.Context, dbItem *ProjectDayEventStatusItemDbItem) error {
	dbItem.DayEventStatusItemId = db.genDayEventStatusItemId(dbItem.ProjectId, dbItem.MemberUserId, dbItem.Day, dbItem.EventType)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ProjectDayEventStatusDao) List(ctx context.Context, projectId, memberUserId string, fromDay, toDay uint32) ([]*ProjectDayEventStatusItemDbItem, error) {
	dayList := []uint32{}
	for day := fromDay; day <= toDay; day++ {
		dayList = append(dayList, day)
	}
	cursor, err := db.mc.Find(ctx,
		bson.M{"projectId": projectId, "memberUserId": memberUserId, "day": bson.M{"$in": dayList}},
		options.Find().SetSort(bson.M{"day": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*ProjectDayEventStatusItemDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_ProjectDayEventStatusDao) Inc(ctx context.Context, projectId, memberUserId string, day uint32, eventType int32) error {
	dayEventStatusItemId := db.genDayEventStatusItemId(projectId, memberUserId, day, eventType)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dayEventStatusItemId, "projectId": projectId}, bson.M{"$inc": bson.M{"count": 1}})
	return err
}
